package phonecompany.account;

import java.io.Serializable;

import phonecompany.call.Call;
import phonecompany.customer.Customer;
import phonecompany.phone.PhoneNumber;

/**
 * @since Oct 24, 2011
 * @author <a href="mailto:zexpp5@gmail.com">Zexu.Lin</a>
 * @version $Id: PhoneAccount.java 28 2011-10-30 21:25:58Z zexpp5@gmail.com $
 * 
 */
public abstract class PhoneAccount implements Account, Serializable {
	private double balance = 0;
	private Customer holder;
	private PhoneNumber phoneNumber;
	private boolean blocked = false;

	private PhoneAccount() {
	}

	/**
	 * To construct a PhoneAccount with customer and phone number.
	 * 
	 * Constructor Method
	 * 
	 * @param holder
	 * @param phoneNumber
	 */
	public PhoneAccount(Customer holder, PhoneNumber phoneNumber) {
		this.holder = holder;
		this.phoneNumber = phoneNumber;
	}

	/**
	 * 
	 * (non-Javadoc)
	 * 
	 * @see phonecompany.account.Account#getNumber()
	 */
	@Override
	public PhoneNumber getNumber() {
		if (phoneNumber == null) {
			System.out
					.println("Phone number haven't been allocated to this account");
		}
		return phoneNumber;
	}

	/**
	 * 
	 * (non-Javadoc)
	 * 
	 * @see phonecompany.account.Account#getBalance()
	 */
	@Override
	public double getBalance() {
		return balance;
	}

	/**
	 * 
	 * (non-Javadoc)
	 * 
	 * @see phonecompany.account.Account#chargeCall(phonecompany.phone.PhoneNumber,
	 *      int)
	 */
	@Override
	public Call chargeCall(PhoneNumber toNumber, int duration) {
		Call c = null;
		try {
			c = new Call(this.phoneNumber, toNumber, duration);
		} catch (Exception e) {
			System.out.println("call failed.");
			e.printStackTrace();
		}
		return c;
	}

	/**
	 * 
	 * (non-Javadoc)
	 * 
	 * @see phonecompany.account.Account#creditAccount(double)
	 */
	@Override
	public boolean creditAccount(double pounds) {
		this.balance += pounds;
		return true;
	}

	/**
	 * 
	 * (non-Javadoc)
	 * 
	 * @see phonecompany.account.Account#getCustomer()
	 */
	@Override
	public Customer getCustomer() {
		if (holder == null) {
			System.out.println("This account haven't been allocated.");
		}
		return holder;
	}

	/**
	 * 
	 * (non-Javadoc)
	 * 
	 * @see phonecompany.account.Account#isBlocked()
	 */
	@Override
	public boolean isBlocked() {
		return blocked;
	}

	/**
	 * 
	 * (non-Javadoc)
	 * 
	 * @see phonecompany.account.Account#block()
	 */
	@Override
	public void block() {
		this.blocked = true;
	}

	/**
	 * 
	 * (non-Javadoc)
	 * 
	 * @see phonecompany.account.Account#getTariff()
	 */
	public abstract int getTariff();
}
